package guda.house.biz.bo.impl;

import guda.house.biz.bo.ElecFeeBO;
import guda.house.biz.query.ElecRuleQuery;
import guda.house.dal.dao.BuildingDAO;
import guda.house.dal.dao.ElecRuleDAO;
import guda.house.dal.dao.SuiteDAO;
import guda.house.dal.dao.UnitDAO;
import guda.house.dal.dataobject.BuildingDO;
import guda.house.dal.dataobject.ElecRuleDO;
import guda.house.dal.dataobject.SuiteDO;
import guda.house.dal.dataobject.UnitDO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * Created by well on 15/11/15.
 */
public class ElecFeeBOImpl implements ElecFeeBO{

    private final static Logger logger = LoggerFactory.getLogger(ElecFeeBOImpl.class);
    @Autowired
    private ElecRuleDAO elecRuleDAO;
    @Autowired
    private UnitDAO unitDAO;
    @Autowired
    private BuildingDAO buildingDAO;
    @Autowired
    private SuiteDAO suiteDAO;

    @Override
    public long countElecFee(Long villageId, Long suiteId,int elecRecord) {
        SuiteDO suiteById = suiteDAO.getSuiteById(suiteId);
       // UnitDO unitById = unitDAO.getUnitById(suiteById.getUnitId());
       // BuildingDO buildingById = buildingDAO.getBuildingById(unitById.getBuildingId());
        ElecRuleQuery elecRuleQuery = new ElecRuleQuery();
        elecRuleQuery.setVillageId(villageId);
        elecRuleQuery.setBuildingTypeId(suiteById.getBuildingTypeId());
        List<ElecRuleDO> elecRuleDOs = elecRuleDAO.selectForView(elecRuleQuery);
        if(elecRuleDOs.size() == 0 ){
            logger.warn("小区ID" + villageId + "没有设置电费规则。。");
            return 0L;
        }
        //从大到小排序
        Collections.sort(elecRuleDOs, new Comparator<ElecRuleDO>() {
            @Override
            public int compare(ElecRuleDO o1, ElecRuleDO o2) {
                return o2.getMinValue() - o1.getMinValue();
            }
        });
        long total = 0l;
        Iterator<ElecRuleDO> iterator = elecRuleDOs.iterator();
        while(iterator.hasNext()){
            ElecRuleDO next = iterator.next();
            if(next.getMaxValue() == null){
                total  = total + (elecRecord - next.getMinValue())*next.getFeeUnit();
                iterator.remove();
                break;
            }
            if(elecRecord< next.getMinValue()){
                iterator.remove();
                continue;
            }
            if(elecRecord >  next.getMinValue() && elecRecord < next.getMaxValue()){
                total  = total + (elecRecord - next.getMinValue())*next.getFeeUnit();
                iterator.remove();
                break;
            }

            if(elecRecord > next.getMaxValue()){
                total  = total + (elecRecord - next.getMaxValue())*next.getFeeUnit();
                iterator.remove();
                break;
            }
        }
        for(ElecRuleDO elec:elecRuleDOs){
            total = total + (elec.getMaxValue()  - elec.getMinValue())*elec.getFeeUnit();
        }

        return total;
    }


    public static void main(String args[]){
        long elecRecord = 100;
        List<ElecRuleDO> elecRuleDOs = new ArrayList<ElecRuleDO>();
        ElecRuleDO elecRuleDO = new ElecRuleDO();
        elecRuleDO.setFeeUnit(1000L);
       // elecRuleDO.setMaxValue(110);
        elecRuleDO.setMinValue(50);
        elecRuleDOs.add(elecRuleDO);

        elecRuleDO = new ElecRuleDO();
        elecRuleDO.setFeeUnit(100L);
        elecRuleDO.setMaxValue(50);
        elecRuleDO.setMinValue(10);
        elecRuleDOs.add(elecRuleDO);

        elecRuleDO = new ElecRuleDO();
        elecRuleDO.setFeeUnit(1L);
        elecRuleDO.setMaxValue(10);
        elecRuleDO.setMinValue(0);
        elecRuleDOs.add(elecRuleDO);


        //从大到小排序
        Collections.sort(elecRuleDOs, new Comparator<ElecRuleDO>() {
            @Override
            public int compare(ElecRuleDO o1, ElecRuleDO o2) {
                return o2.getMinValue() - o1.getMinValue();
            }
        });
        long total = 0l;
        Iterator<ElecRuleDO> iterator = elecRuleDOs.iterator();
        while(iterator.hasNext()){
            ElecRuleDO next = iterator.next();
            if(next.getMaxValue() == null){
                total  = total + (elecRecord - next.getMinValue())*next.getFeeUnit();
                iterator.remove();
                break;
            }
            if(elecRecord< next.getMinValue()){
                iterator.remove();
                continue;
            }
            if(elecRecord >  next.getMinValue() && elecRecord < next.getMaxValue()){
                total  = total + (elecRecord - next.getMinValue())*next.getFeeUnit();
                iterator.remove();
                break;
            }

            if(elecRecord > next.getMaxValue()){
                total  = total + (elecRecord - next.getMaxValue())*next.getFeeUnit();
                iterator.remove();
                break;
            }
        }
        for(ElecRuleDO elec:elecRuleDOs){
            total = total + (elec.getMaxValue()  - elec.getMinValue())*elec.getFeeUnit();
        }
        System.out.println(total);
    }
}
